package com.itheima.day10;

import com.itheima.domain.Student;
import com.itheima.util.RandomUtil;
import com.sun.source.tree.Tree;

import java.time.LocalDate;
import java.time.Period;
import java.util.*;
import java.util.stream.Collectors;

/*
1_ 第7天遗留统计
各个小组的平均年龄？
最大三个年纪的
最小三个年纪的
最大年纪的人和最小年纪的人是谁，差多久?
不同的姓各自有多少学生？
不同的年龄段有多少人？

 */
public class Test01 {

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();


        for (int i = 1; i <= 32; i++) {
            Student student = new Student();
            if (i <= 9) {
                student.setSid("A2021101900" + i);
            } else {
                student.setSid("A202110190" + i);
            }
            student.setName(RandomUtil.randomName());
            student.setGender(RandomUtil.randomGender());
            student.setBirthday(RandomUtil.randomBirthday());
            student.setGroup((i - 1) / 4 + 1);
            student.setPhone(RandomUtil.randomPhone());
            student.setWxs(RandomUtil.randomWxs());
            list.add(student);
        }
        for (Student student : list) {
            System.out.println(student);
        }
        //groupAvgAge(list);
        //get3Youngest(list);
        //get3Oldest(list);
        //getOldestAndYoungest(list);
        //getXingFenbu(list);
        getAgeFenbu(list);
    }

    //各个小组的平均年龄？
    public static void groupAvgAge(ArrayList<Student> list) {
        //1.生成32个人 放入ArrayList 中
        //2.遍历ArrayList  将 所有人分组 放入 TreeMap中  键是组名  值是组内的人
        TreeMap<Integer, ArrayList<Student>> map = new TreeMap<>();
        for (Student student : list) {
            int group = student.getGroup();
            if (map.containsKey(group)) {//map中已经有这个组了
                //直接加到这个组的 集合中即可
                ArrayList<Student> list1 = map.get(group);
                list1.add(student);
            } else {
                ArrayList<Student> list1 = new ArrayList<>(List.of(student));
                map.put(group, list1);
            }
        }

        TreeMap<Integer, Integer> ageMap = new TreeMap<>();

        //3.遍历 TreeMap  统计每个组内的平均年龄 放入 新的treeMap中  键是组名  值是 平均年龄
        map.forEach((k, v) -> {
            //统计本组的平均年龄
            int sum = 0;
            for (Student student : v) {
                sum += student.getAge();
            }
            int age = sum / v.size();
            ageMap.put(k, age);

        });

        //4遍历TreeMap 拿到结果
        ageMap.forEach((k, v) -> System.out.println("第" + k + "组的平均年龄是" + v)
        );

    }

    //最大三个年纪的
    //最小三个年纪的

    public static void get3Youngest(ArrayList<Student> list){
        //创建TreeSet 装学生 按照生日 的倒叙 否则按姓名
        TreeSet<Student> treeSet = new TreeSet<>((o1, o2) -> {
            LocalDate birthday1 = o1.getBirthday();
            LocalDate birthday2 = o2.getBirthday();
            int i = birthday2.compareTo(birthday1);
            return i == 0 ? o1.getName().compareTo(o2.getName()) : i;
        });
        treeSet.addAll(list);
        List<Student> collect = treeSet.stream().limit(3).collect(Collectors.toList());
        System.out.println("三个最小的是:");
        for (Student student : collect) {
            System.out.println(student);
        }
    }
    public static void get3Oldest(ArrayList<Student> list){
        //创建TreeSet 装学生 按照生日 的倒叙 否则按姓名
        TreeSet<Student> treeSet = new TreeSet<>((o1, o2) -> {
            LocalDate birthday1 = o1.getBirthday();
            LocalDate birthday2 = o2.getBirthday();
            int i = birthday1.compareTo(birthday2);
            return i == 0 ? o1.getName().compareTo(o2.getName()) : i;
        });
        treeSet.addAll(list);
        List<Student> collect = treeSet.stream().limit(3).collect(Collectors.toList());
        System.out.println("三个最老的是:");
        for (Student student : collect) {
            System.out.println(student);
        }
    }

        //求最老和最小
    public static void  getOldestAndYoungest(ArrayList<Student> list){
        TreeSet<Student> treeSet = new TreeSet<>((o1, o2) -> {
            LocalDate birthday1 = o1.getBirthday();
            LocalDate birthday2 = o2.getBirthday();
            int i = birthday1.compareTo(birthday2);
            return i == 0 ? o1.getName().compareTo(o2.getName()) : i;
        });
        treeSet.addAll(list);

        Student first = treeSet.first();
        Student last = treeSet.last();
        System.out.println("最老的"+first);
        System.out.println("最小的"+last);
        Period between = Period.between(first.getBirthday(), last.getBirthday());
        System.out.println("俩人差"+between.getYears()+"年"+between.getMonths()+"月"+between.getDays()+"天");
    }

    //统计姓氏
    public static void  getXingFenbu(ArrayList<Student> list){
        //两个map
        HashMap<String ,ArrayList<Student>> map=new HashMap<>();
        for (Student student : list) {
            char key = student.getName().charAt(0);

            if(map.containsKey(key+"")){
                ArrayList<Student> list1 = map.get(key + "");
                list1.add(student);
            }else {
                ArrayList<Student> list1 = new ArrayList<>(List.of(student));
                map.put(key+"",list1);
            }
        }
         //
        HashMap<String ,Integer> xingMap=new HashMap<>();
        map.forEach((k,v)->{
            int count=0;
            for (Student student : v) {
                count++;
            }
            xingMap.put(k,count);
        });
        xingMap.forEach((k,v)-> System.out.println("姓"+k+"的有"+v+"个人"));


    }


    //统计姓氏
    public static void  getAgeFenbu(ArrayList<Student> list){
        //两个map
        HashMap<Integer ,ArrayList<Student>> map=new HashMap<>();
        for (Student student : list) {
            int age= student.getAge();

            if(map.containsKey(age)){
                ArrayList<Student> list1 = map.get(age);
                list1.add(student);
            }else {
                ArrayList<Student> list1 = new ArrayList<>(List.of(student));
                map.put(age,list1);
            }
        }
        //
        HashMap<Integer ,Integer> ageMap=new HashMap<>();
        map.forEach((k,v)->{
            //统计本组的平均年龄
            int count=0;
            for (Student student : v) {
                count++;
            }
            ageMap.put(k,count);

        });
        ageMap.forEach((k,v)-> System.out.println("年龄是"+k+"的有"+v+"个人"));


    }


}
